package com.enjoyor.soa.traffic.restt.tpcms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.tpcms.PassApplyDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.IPassApplyConsumeService;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.invoke.UbmsDeptBayonetServiceInvoke;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/passApply" })
@Api("申请录入Controller页面请求")
@CrossOrigin
public class PassApplyController {

    @Autowired
    private IPassApplyConsumeService passApplyConsumeService;
    
    @Autowired
    private UbmsDeptBayonetServiceInvoke ubmsDeptService;

    /**
     * 新增申请单
     * 
     * @param passApplyDto
     *            申请单实体
     */
    @PostMapping(value = { "/insertPassApply" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("新增申请单")
    public ResultPojo insertPassApply(@RequestBody PassApplyDto passApplyDto, HttpServletRequest request) {
        return passApplyConsumeService.insertPassApply(passApplyDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 新增并提交申请单
     * 
     * @param passApplyDto
     *            申请单实体
     */
    @PostMapping(value = { "/insertSubmitPassApply" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("新增并提交申请单")
    public ResultPojo insertSubmitPassApply(@RequestBody PassApplyDto passApplyDto, HttpServletRequest request) {
        return passApplyConsumeService.insertSubmitPassApply(passApplyDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 删除申请单
     */
    @DeleteMapping(value = { "/deletePassApply" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("删除申请单")
    @ApiImplicitParam(name = "passApplyId", value = "申请单编号", paramType = "query", required = true, dataType = "String")
    public ResultPojo deletePassApply(@RequestParam(value = "passApplyId", required = true) String passApplyId,
            HttpServletRequest request) {
        return passApplyConsumeService.deletePassApply(passApplyId,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 修改申请单
     * 
     * @param passApplyDto
     *            申请单实体
     */
    @PutMapping(value = { "/editPassApply" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("修改申请单")
    public ResultPojo editPassApply(@RequestBody PassApplyDto passApplyDto, HttpServletRequest request) {
        return passApplyConsumeService.editPassApply(passApplyDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 修改并提交申请单
     * 
     * @param passApplyDto
     *            申请单实体
     */
    @PutMapping(value = { "/editSubmitPassApply" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("修改并提交申请单")
    public ResultPojo editSubmitPassApply(@RequestBody PassApplyDto passApplyDto, HttpServletRequest request) {
        return passApplyConsumeService.editSubmitPassApply(passApplyDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 申请单提交审核
     */
    @PutMapping(value = { "/submitPassApply" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("申请单提交审核")
    @ApiImplicitParam(name = "passApplyId", value = "申请单编号", paramType = "query", required = true, dataType = "String")
    public ResultPojo submitPassApply(
            @RequestParam(defaultValue = "", value = "passApplyId", required = true) String passApplyId,
            HttpServletRequest request) {
        return passApplyConsumeService.submitPassApply(passApplyId,((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 查询申请单列表信息List
     * 
     * @param passApplyId
     *            申请编号（模糊查询）
     * @param applyPeople
     *            申请人（模糊查询）
     * @param applyDept
     *            所属单位（模糊查询）
     * @param applyTypeId
     *            申请种类
     * @param applyStartTime
     *            申请开始时间(yyyy-mm-dd hh24:mi:ss)
     * @param applyEndTime
     *            申请结束时间(yyyy-mm-dd hh24:mi:ss)
     * @param applyStatusId
     *            申请状态编号
     * @return
     */
    @GetMapping(value = { "/getPassApplyList" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "获取申请单信息", notes = "查询数据库中符合条件的申请单数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "passApplyId", value = "申请编号（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyPeople", value = "申请人（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyDept", value = "所属单位（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyTypeId", value = "申请种类", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyStartTime", value = "申请开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyEndTime", value = "申请结束时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyStatusId", value = "申请状态编号", paramType = "query", dataType = "String") })
    public ResultPojo getPassApplyList(@RequestParam(defaultValue = "") String passApplyId,
            @RequestParam(defaultValue = "") String applyPeople, @RequestParam(defaultValue = "") String applyDept,
            @RequestParam(defaultValue = "") String applyTypeId, @RequestParam(defaultValue = "") String applyStartTime,
            @RequestParam(defaultValue = "") String applyEndTime, @RequestParam(defaultValue = "") String applyStatusId,
            HttpServletRequest request) {
        return passApplyConsumeService.getPassApplyList(passApplyId, applyPeople, applyDept, applyTypeId,
                applyStartTime, applyEndTime, applyStatusId);
    }

    /**
     * 查询申请单分页信息
     * 
     * @param page
     *            分页参数
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @GetMapping(value = { "/getPassApplyPage" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "申请单分页查询", notes = "申请单分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "passApplyId", value = "申请编号（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyPeople", value = "申请人（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyDept", value = "所属单位（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyTypeId", value = "申请种类", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyStartTime", value = "申请开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyEndTime", value = "申请结束时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applyStatusId", value = "申请状态编号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "licenseNumber", value = "车牌号码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dataDept", value = "数据权限部门", paramType = "query", dataType = "String") })
    public ResultPojo getPassApplyPage(@RequestParam(defaultValue = "") String passApplyId,
            @RequestParam(defaultValue = "") String applyPeople, @RequestParam(defaultValue = "") String applyDept,
            @RequestParam(defaultValue = "") String applyTypeId, @RequestParam(defaultValue = "") String applyStartTime,
            @RequestParam(defaultValue = "") String applyEndTime, @RequestParam(defaultValue = "") String applyStatusId,
            @RequestParam(defaultValue = "") String licenseNumber, @RequestParam(defaultValue = "") String dataDept, 
            Page page, HttpServletRequest request) {
        Map<String, Object> mapBean = new HashMap<String, Object>();
        mapBean.put("passApplyId", passApplyId);
        mapBean.put("applyPeople", applyPeople);
        mapBean.put("applyDept", applyDept);
        mapBean.put("applyTypeId", applyTypeId);
        mapBean.put("applyStartTime", applyStartTime);
        mapBean.put("applyEndTime", applyEndTime);
        mapBean.put("applyStatusId", applyStatusId);
        mapBean.put("licenseNumber", licenseNumber);
        if(null != dataDept && dataDept != "") {
        	List<String> dataDeptIds = ubmsDeptService.getDeptsByParentId(dataDept);
            mapBean.put("dataDeptIds", dataDeptIds);
        }
        page.setMapBean(mapBean);
        return passApplyConsumeService.getPassApplyPage(page);
    }

}
